/**
 * @(#) CountryProxy.as
 *
 * @date		2009. 02. 18.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 주소변경 - Proxy
 * 				
 */
package com.skt.common_hr.main.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.main.model.vo.SiteLinkVO;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class SiteLinkProxy extends CommonProxy
	{
		public static const NAME:String = "SiteLinkProxy";
		
		public static const LOAD_SITELINK_COMPLETE:String = "loadSiteLinkComplete";
		public static const LOAD_SITELINK_FAILED:String	 = "loadSiteLinkFailed";
		
		public var links:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function SiteLinkProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		public function load():void
		{
			var param:Object = new Object();
			
			loadData("Z_HR_0111",param, result, fault); 
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {
				links = parseSiteLinks(event.result["ITAB"]);		
				facade.sendNotification(LOAD_SITELINK_COMPLETE, this);
			}
			else {
				links = new ArrayCollection();
				links.addItem(new SiteLinkVO("SiteLink", ""));
				facade.sendNotification(LOAD_SITELINK_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			links = new ArrayCollection();
			links.addItem(new SiteLinkVO("SiteLink", ""));
			facade.sendNotification(LOAD_SITELINK_FAILED, this);
		}
		
		private function parseSiteLinks(src:ArrayCollection):ArrayCollection
		{
			var target:ArrayCollection = new ArrayCollection();
			target.addItem(new SiteLinkVO("SiteLink", ""));
			for each(var rec:Object in src) {
				target.addItem(new SiteLinkVO(rec.SITEN, rec.URL));
			}
			return target;
		}
	}
}